package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.core.code.FixedDict;
import com.pactera.jep.core.spring.FixedDictPostProcessor;
import com.pactera.jep.web.controller.FixedDictController;
import com.pactera.jep.web.entity.BasicNameValuePair;
import com.pactera.jep.web.entity.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fixedDict")
public class FixedDictRestController {
    Logger logger = LoggerFactory.getLogger(FixedDictController.class);

    @Autowired
    FixedDictPostProcessor fixedDictProcessor;

    /**
     * 用于table或列表
     * @param name
     * @param includes
     * @param excludes
     * @return
     */
    @GetMapping(value = "/name")
    @ResponseBody
    public List<NameValuePair> query(@PathVariable String name, String includes, String excludes) {
        return queryFixedDict(name, includes, excludes);
    }

    /**
     * 查询某个fixedDict的值列表，返回 namevaluepair
     * @param name
     * @param includes
     * @param excludes
     * @return
     */
    private List<NameValuePair> queryFixedDict(String name, String includes, String excludes)
    {
        List<NameValuePair> items = new ArrayList<>();
        try
        {
            Map<String, Class<?>> dictClass = fixedDictProcessor.getFixedDictClass();
            //获取simpleName对应的枚举类
            Class<?> clazz = dictClass.get(name);
            if (clazz != null) {

                FixedDict dictAnnotation = AnnotationUtils.findAnnotation(clazz, FixedDict.class);
                Class<?>[] args = new Class<?>[]{};
                List<String> lstExcludes = StringUtils.isNotBlank(excludes) ? Arrays.asList(excludes.split(",")) : null;
                List<String> lstIncludes = StringUtils.isNotBlank(includes) ? Arrays.asList(includes.split(",")) : null;

                //通过枚举类获取枚举对象列表
                List<?> list = Arrays.asList(clazz.getEnumConstants());
                for (Object item : list) {

					/*excludes 包含的先排除*/
                    if (com.pactera.jep.commons.CollectionUtils.isNotEmpty(lstExcludes) && lstExcludes.contains(item.toString())) {
                        continue;
                    }

					/*includes 不包含的后排除*/
                    if (com.pactera.jep.commons.CollectionUtils.isNotEmpty(lstIncludes) && !lstIncludes.contains(item.toString())) {
                        continue;
                    }

                    Method mLabel = clazz.getMethod(dictAnnotation.label(), args);
                    Method mValue = clazz.getMethod(dictAnnotation.value(), args);
                    BasicNameValuePair nv = new BasicNameValuePair((String)mLabel.invoke(item, new Object[]{}), (String)mValue.invoke(item, new Object[]{}));
                    items.add(nv);
                }
            }

        } catch(Exception ex)
        {
            logger.error(ex.getMessage(), ex);
            ex.printStackTrace();
        }
        return items;
    }
}
